//
//  LaunchView.m
//  XPhoto
//
//  Created by sentinel on 2020/5/7.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "LaunchView.h"
#import <BKKit/UIView+x.h>
@interface LaunchView()
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, assign) NSInteger remainSeconds;
@end

@implementation LaunchView
-(void)awakeFromNib{
    [super awakeFromNib];
}
- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    if ([self superview]) {
        [self startCountDown];
    }
}
- (void)setRemainSeconds:(NSInteger)remainSeconds{
    _remainSeconds = remainSeconds;
    [self.timeButton setTitle:[NSString stringWithFormat:NSLocalizedString(@"launch_count_down_fmt", @"描述"), remainSeconds] forState:UIControlStateNormal];
}
- (void)startCountDown{
    self.remainSeconds = 10;
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
        self.remainSeconds --;
        if (self.remainSeconds <= 0) {
            [self stopCountDown];
        }
    }];
}
- (void)stopCountDown{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    [self dismiss];
}

- (IBAction)skip:(id)sender{
    [self stopCountDown];
}
- (void)dismiss{
    if ([self superview]) {
        [self removeFromSuperview];
    }
    [self.delegate launchViewDidDismissed:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (LaunchView *)view{
    LaunchView *view = loadViewFromNib([self class], nil);
    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    return view;
}
@end
